<template>
  <!-- 项目选择 -->
  <a-select
    v-model="currentValue"
    v-bind="$attrs"
    :placeholder="placeholder"
    v-on="$listeners"
  >
    <a-icon
      v-if="fetching"
      slot="suffixIcon"
      style="color: #096dd9"
      type="loading"
    />
    <a-spin
      v-if="fetching"
      slot="notFoundContent"
      size="small"
    />
    <a-select-option
      v-for="i in orgList"
      :key="i.id"
      :value="i.id"
    >
      {{ i.unionOrgName }}
    </a-select-option>
  </a-select>
</template>
<script>
import { getUnionOrganizationList } from '@/api/union/organizationManagement'

export default {
    name: 'SelectProject',
    props: {
        // v-model绑定的数据
        value: {
            type: [Array, String, Object, Number],
        },
        // 需要联动的表单名，
        form: {
            type: Object,
            default: () => ({}),
        },
        // placeholder
        placeholder: {
            type: String,
            default: () => '请选择',
        },
        // 需要联动的表单名
        keyValue: {
            type: String,
            default: () => 'unionLevelId',
        },
        // 联动表单值改变时是否清空当前表单值
        isClear: {
            type: Boolean,
            default: () => true,
        },

        //如果是编辑，第一次不要重置绑定的值。。。。
        isEdit: {
            type: Boolean,
            default: () => false,
        },

        // 是否自动选中第一行
        autoSelected: {
            type: Boolean,
            default: () => true,
        },
        isAuth: {
            type: Number,
            default: () => 1,
        },
    },
    mounted() {
        // this.getList()
    },
    data() {
        return {
            isCanClear: !this.isEdit, // 是否清除
            fetching: false, // 加载控件
            orgList: [], // 项目列表
        }
    },
    methods: {
        /**
         * 获取项目列表
         */
        getList() {
            let params = {
                unionLevelId:
                    this.form[this.keyValue] == '605004401341501446' ? '605004340528287745' : '605004401341501446',
                auditStatus: 'PAST',
                isAuth: this.isAuth,
            }
            this.fetching = true
            getUnionOrganizationList(params).then((res) => {
                this.fetching = false
                this.orgList = res.data
                if(this.form[this.keyValue] =='605004401341501446'){
                     this.currentValue = this.orgList[0].id
                }
                //
            })
        },
    },
    computed: {
        // 需要联动的表单值
        attr() {
            return this.form[this.keyValue]
        },
        // 转换绑定的选中数据，与v-model同步
        currentValue: {
            get: function () {
                this.orgList.length > 0 &&
                    this.value &&
                    this.$emit(
                        'changeOrg',
                        this.orgList.find((item) => item.id === this.value)
                    )
                return this.value
            },
            set: function (value) {
                this.$emit('input', value)
            },
        },
    },
    watch: {
        attr(val) {
            // 清空表单
            if (this.isClear && !this.isEdit) {
                this.currentValue = void 0
                this.$emit('resetSelf') // 在搜索组件(SearchData)中重置自身的值
            }
            // 清空表单
            if (this.isEdit && this.isCanClear) {
                this.currentValue = void 0
                this.$emit('resetSelf') // 在搜索组件(SearchData)中重置自身的值
            } else {
                this.isCanClear = true
            }
            // 重置列表
            this.orgList.length = 0
            if (val == '605004340528287745') {
                this.orgList = [
                    {
                        unionOrgName: '局工会',
                        id: '0',
                    },
                ]
                this.currentValue = '0'
                return
            }
            // if (this.form.parentId != 0) {
            console.log(this.form, this.form.parentId)
            this.$set(this.form, 'parentId', '')
            this.$set(this.form, 'orgIds', [])
            this.form.unionChairmanId = ''
            this.$set(this.form, 'unionChairmanId', '')
            // }
            console.log(this.form)
            val && this.getList()
        },
    },
}
</script>
